﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Jack.EtcdConfiguration
{
    internal static class EtcdHelper
    {
        private const string rangeEndString = "\x00";
        public static string GetRangeEnd(string prefixKey)
        {
            if (prefixKey.Length == 0)
            {
                return rangeEndString;
            }

            StringBuilder rangeEnd = new(prefixKey);
            rangeEnd[rangeEnd.Length - 1] = ++rangeEnd[rangeEnd.Length - 1];
            return rangeEnd.ToString();
        }

        public static bool IsJsonString(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return false; // 空字符串或仅包含空白字符，不是有效 JSON
            }

            // JSON 正则表达式模式，支持对象和数组
            var pattern = @"^\s*(\{.*\}|

\[.*\]

)\s*$"; // 支持多行匹配

            // 使用 RegexOptions.Singleline 使得正则支持换行符
            return Regex.IsMatch(input, pattern, RegexOptions.Singleline);
        }
    }
}
